package com.zoo.buyor.user.service.impl;

import com.zoo.buyor.common.event.UserRegisterEvent;
import com.zoo.buyor.common.utils.AssertUtil;
import com.zoo.buyor.common.utils.algorithm.sensitiveWord.SensitiveWordBs;
import com.zoo.buyor.user.adapter.UserAdapter;
import com.zoo.buyor.user.cache.ItemCache;
import com.zoo.buyor.user.dao.ItemConfigDao;
import com.zoo.buyor.user.dao.UserBackpackDao;
import com.zoo.buyor.user.dao.UserDao;
import com.zoo.buyor.user.domain.entity.ItemConfig;
import com.zoo.buyor.user.domain.entity.User;
import com.zoo.buyor.user.domain.entity.UserBackpack;
import com.zoo.buyor.user.domain.enums.ItemEnum;
import com.zoo.buyor.user.domain.enums.ItemTypeEnum;
import com.zoo.buyor.user.domain.vo.request.ModifyNameReq;
import com.zoo.buyor.user.domain.vo.response.user.BadgeResp;
import com.zoo.buyor.user.domain.vo.response.user.UserInfoResp;
import com.zoo.buyor.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserBackpackDao userBackpackDao;
    @Autowired
    private SensitiveWordBs sensitiveWordBs;

    @Autowired
    private ItemConfigDao itemConfigDao;

    @Autowired
    private ItemCache itemCache;
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Override
    @Transactional
    public Long register(User userInsert) {
        boolean result = userDao.save(userInsert);
        //todo 订阅业务完善
        applicationEventPublisher.publishEvent(new UserRegisterEvent(this,userInsert));
        return result ? userInsert.getId() : null;
    }

    @Override
    public UserInfoResp getUserInfo(Long uid) {
        User user = userDao.getById(uid);
        //改名卡的剩余次数
        Integer countOfRenameCard = userBackpackDao.getItem(uid, ItemEnum.MODIFY_NAME_CARD.getId());
        return UserAdapter.buildUserInfoResp(user, countOfRenameCard);
    }

    @Override
    @Transactional
    public void modifyName(Long uid, ModifyNameReq nameReq) {
        //判断名字是不是重复
        String newName = nameReq.getName();
        AssertUtil.isFalse(sensitiveWordBs.hasSensitiveWord(newName), "名字中包含敏感词，请重新输入"); // 判断名字中有没有敏感词
        User oldUser = userDao.getByName(newName);
        AssertUtil.isEmpty(oldUser, "名字已经被抢占了，请换一个哦~~");
        //判断改名卡够不够
        UserBackpack firstValidItem = userBackpackDao.getOneValidItem(uid, ItemEnum.MODIFY_NAME_CARD.getId());
        AssertUtil.isNotEmpty(firstValidItem, "改名次数不够了，等后续活动送改名卡哦");
        //使用改名卡
        boolean useSuccess = userBackpackDao.invalidItem(firstValidItem.getId());
        if (useSuccess) {
            //改名
            userDao.modifyName(uid, nameReq.getName());
            //删除缓存
            //userCache.userInfoChange(uid);
        }
    }

    @Override
    public List<BadgeResp> badges(Long uid) {
        //查询所有徽章
        List<ItemConfig> itemConfigs = itemCache.getByType(ItemTypeEnum.BADGE.getType());
        //查询用户拥有的徽章
        List<UserBackpack> backpacks = userBackpackDao.getByItemIds(uid, itemConfigs.stream().map(ItemConfig::getId).collect(Collectors.toList()));
        //查询用户当前佩戴的徽章
        User user = userDao.getById(uid);
        return UserAdapter.buildBadgeResp(itemConfigs,backpacks,user);

    }
}
